<?php
/*
$JA#COPYRIGHT$
*/

// no direct access
defined('_JEXEC') or die('Restricted access');
require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');

class modLatestViewJobsHelper
{
	var $_fieldList;
	
	function get_JobId()
	{
		$job_id = array();
		if (isset($_COOKIE["viewjob_count"]))
			$viewjob_count = $_COOKIE["viewjob_count"];
		else 
			$viewjob_count = 0;
			
		for ($i=1;$i<=$viewjob_count;$i++){		    
		    if(isset($_COOKIE['view_'.$i])) $job_id[] = $_COOKIE["view_".$i];
		}

		return $job_id;
	}
	
	
	
	function get_list_jobs(&$params)
	{
		
		global $mainframe;

		$db			=& JFactory::getDBO();
		$user		=& JFactory::getUser();
		$userId		= (int) $user->get('id');

		$job_id = modLatestViewJobsHelper::get_JobId();
		$job_id = modLatestViewJobsHelper::cleandata($job_id);
		
		$count		= (int) $params->get('count', 5);
        $rows = array();
        $model = new JobBoardModelJAJobs();
        
		for ($i=0;$i<count($job_id);$i++)
		{
			if ($job_id[$i]!='')
				$where_more = " and a.id = $job_id[$i] AND a.status = 'Approved' ";
			else 
				$where_more = '';
			JRequest::setVar('module_recent_viewed_job',1);
			$temp = $model->getItems($where_more, $count, 0);
			JRequest::setVar('module_recent_viewed_job',0);
			if (count($temp)>0)
				$rows[] = $temp[0];
			if (count($rows)==$count)
				break;
		}
        
		return $rows;	
	}
	
	function cleandata($job_id)
	{
		$temp = array();
		if (is_array($job_id))
		{
			for ($i=count($job_id)-1;$i>=0;$i--)
			{
				
				if ($job_id[$i])
				{
					$is_exist = false;
					for ($j=0;$j<count($temp);$j++)
						if ($job_id[$i]==$temp[$j])
							$is_exist = true;
					if (!$is_exist)
						$temp[] = $job_id[$i];
				}
			}		
		}
		return $temp;
	}
	
	
}